package com.example;

import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.*;

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static String masterAddresses = "linux122";
    private static String tableName = "student";
    //client
    private static KuduClient.KuduClientBuilder kuduClientBuilder = new
            KuduClient.KuduClientBuilder(masterAddresses);
    private static KuduClient client = kuduClientBuilder
            .defaultSocketReadTimeoutMs(6000)
            .build();
    
    public static void main(String[] args) {

    }


    /**
     * （1）必须指定表连接到的master节点主机名
     * （2）必须定义schema,声明的每一个字段必须显式的说明是否是主键
     * （3）必须指定副本数量、分区策略和数量
     */
    public static void createTable() {
        //id int pkey name string
        //指定每一列的信息
        List<ColumnSchema> columnSchemas = new ArrayList<ColumnSchema>();
        ColumnSchema id = new ColumnSchema.ColumnSchemaBuilder("id",
                Type.INT32).key(true).build();
        ColumnSchema name = new ColumnSchema.ColumnSchemaBuilder("name",
                Type.STRING).key(false).build();
        columnSchemas.add(id);
        columnSchemas.add(name);
        Schema schema = new Schema(columnSchemas);
        CreateTableOptions options = new CreateTableOptions();

        //设定当前的副本数量为1
        options.setNumReplicas(1);
        ArrayList<String> colrule = new ArrayList<String>();
        colrule.add("id");
        options.addHashPartitions(colrule, 3);
        try {
            client.createTable(tableName, schema, options);
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }

    public static void deleteTable() {
        try {
            client.deleteTable("student");
        } catch (KuduException e) {
            e.printStackTrace();
        }finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }

    public static void insertToTable() {
        try {
            KuduTable stuTable = client.openTable("student");
            KuduSession kuduSession = client.newSession();
            kuduSession.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
            Insert insert = stuTable.newInsert();
            insert.getRow().addInt("id",1);
            insert.getRow().addString("name","lucas");
            kuduSession.flush();
            kuduSession.apply(insert);
            kuduSession.close();
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }

    public static void select() {
        try {
            KuduTable stuTable = client.openTable("student");
            KuduScanner scanner = client.newScannerBuilder(stuTable).build();
            while(scanner.hasMoreRows()) {
                for(RowResult result : scanner.nextRows()) {
                    int id = result.getInt("id");
                    String name = result.getString("name");
                    System.out.println(id + name);
                }
            }
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }

    public static void update() {
        try {
            KuduTable stuTable = client.openTable("student");
            KuduSession kuduSession = client.newSession();
            kuduSession.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
            Update update = stuTable.newUpdate();
            update.getRow().addInt("id",1);
            update.getRow().addString("name","xiaoming");
            kuduSession.apply(update);
            kuduSession.close();
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }

    public static void deleteData() {
        try {
            KuduTable stuTable = client.openTable("student");
            KuduSession kuduSession = client.newSession();
            kuduSession.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
            Delete delete = stuTable.newDelete();
            PartialRow row = delete.getRow();
            row.addInt("id",1);
            kuduSession.flush();
            kuduSession.apply(delete);
            kuduSession.close();
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }
}
